const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fictitious: "",
    status: "0",
    list:[],
    id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({id:options.id})
    this.getData()
  },
  onChangeNum(e){
    this.setData({
      fictitious:e.detail
    })
  },
  submit(){
    
    let _arr = [];
    this.data.list.forEach(item => {
      _arr.push({
        "category_id":item.id,
        "tel_content":{
          "amount":item.amount
        },
        "line_content":{
          "amount":item.online,
          "day":item.day
        }
      })
    })
    let json = {
      id: this.data.id,
      form: {
        fictitious: this.data.fictitious,
        status: this.data.status,
        data: _arr
      }
    };
    app._postNetWork({
      url:app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.lawyer.serviceSave"),
      data:json,
      success:(res)=>{
        if(res.data.result == 1){
          app.tips(res.data.msg)
          wx.navigateBack()
        }else{
          app.tips(res.data.msg)
        }
      }
    })
  },
  getData(){
    app._getNetWork({
      url:app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.lawyer.serviceShow"),
      data:{id:this.data.id},
      success:(res)=>{
        if(res.data.result == 1){
          let category_data =  res.data.data.lawyer.category_data;
          category_data.forEach(item => {
            item.amount = "";
            item.online = "";
            item.day = "";
          });
          this.setData({
            fictitious:res.data.data.lawyer.fictitious,
            status:res.data.data.lawyer.status+'',
            list:category_data,
          })
          if(res.data.data.lawyer.service!==null){
            let _data = res.data.data.lawyer.service.data;
            let Ids = [];
            let _arr = []
              category_data.forEach((item,index) => {
                Ids.push(item.id);
            })
            _data.forEach(item=>{
                if(Ids.includes(item.category_id)){
                    _arr.push({
                      id:item.category_id,
                      amount:item.tel_content.amount,
                      online:item.line_content.amount,
                      day:item.line_content.day
                    })
                }
            })
            _arr.forEach(item => {
                this.data.list.forEach(item2 => {
                  if(item.id == item2.id){
                     item2.amount = item.amount;
                    item2.online = item.online;
                    item2.day = item.day;
                  }
                })
            })
            this.setData({list:this.data.list})
          }

        }
      }
    })
  },
  onChange(event){
    let value = event.detail.replace(/[^0-9]/g,''), 
    type = event.currentTarget.dataset.value,
    index =  event.currentTarget.dataset.index;
    this.data.list[index][type] = value;
    this.setData({list:this.data.list})
  },
  onChangeStatus(event) {
    this.setData({
      status: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})